//
//  YYXCategoryView.m
//  33-严雅兴
//
//  Created by YanYanxing on 2017/2/23.
//  Copyright © 2017年 yanyaxing. All rights reserved.
//

#import "YYXCategoryView.h"
#import "Masonry.h"
#import "CZAdditions.h"

@interface YYXCategoryView ()



@end

@implementation YYXCategoryView{
    
    UIView *_lineView;
    
    UIButton *_selectBtn;//用来记录上一次被选中的按钮
    
    NSMutableArray<UIButton *> *_btnList;
    
}


//setter方法被调用就证明偏移数据传过来了，传过来以后就在这个方法里给小黄条改位置
-(void)setOffsetX:(CGFloat)offsetX{
    
    _offsetX = offsetX;
    
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.equalTo(self).offset(offsetX);
    }];
    
    [self layoutIfNeeded];//约束添加完，立即更新frame
    
    NSInteger index = _lineView.frame.origin.x / _lineView.bounds.size.width;
    
    //先让原来选中的button变为NO
    _selectBtn.selected = NO;
    //按钮数组里的某个元素
    _btnList[index].selected = YES;
    
    _selectBtn = _btnList[index];
}

#pragma mark - 点击了分类按钮
-(void)changeCategory:(UIButton *)sender{
    
    //先清除原来选中按钮的选中状态
    _selectBtn.selected = NO;
    
    sender.selected = YES;
    
    _selectBtn = sender;
    
    
    //如何让黄色小条子移动？sender的tag值是几，就让条子移动几*宽度的值
    CGFloat offsetX = sender.tag * sender.bounds.size.width;
    
    [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self).offset(offsetX);
    }];
    
    //自动布局如何实现动画效果？？
    [UIView animateWithDuration:0.4 animations:^{
        
        //是根据约束立即计算frame并且设置
        [self layoutIfNeeded];
    }];
    
    //把当前被点击的按钮的索引，赋值给整个视图的tag
    self.tag = sender.tag;
    
    //发送一条事件
    //相当于告诉之前跟我打招呼的那个人，我现在被点了
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

#pragma mark - 加载界面
-(void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    //创建3个button
    NSArray *titleArr = @[ @"接诊条件",@"医生简介",@"预约时间" ];
    
    NSMutableArray<UIButton *> *btnList = [NSMutableArray array];
    
    NSInteger idx = 0;
    for (NSString *title in titleArr) {
        
        UIButton *btn = [UIButton cz_textButton:title fontSize:14 normalColor:JHSMainColor selectedColor:JHSMainColor];
        
        btn.tag = idx++;//赋完值再+1
        
        [self addSubview:btn];
        [btnList addObject:btn];
        
        [btn addTarget:self action:@selector(changeCategory:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    _btnList = btnList;
    
    [btnList mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    
    [btnList mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(self);
    }];
    
    
    
    //MARK: 创建白色条子
    UIView *lineView = [UIView new];
//    lineView.backgroundColor = [UIColor cz_colorWithHex:0xffd900];
    lineView.backgroundColor=[UIColor whiteColor];
    [self addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.equalTo(self);
        make.width.equalTo(btnList[0]);
        make.height.mas_equalTo(4);
    }];
    
    _lineView = lineView;
    
    //一启动默认让第0个按钮被选中，并且把选中按钮记录成第0个按钮
    btnList[0].selected = YES;
    _selectBtn = btnList[0];
}


@end
